package com.andaily.domain.dto.account;

import com.andaily.domain.account.AccountOverviewShowType;
import com.andaily.domain.account.AccountOverviewSortType;
import com.andaily.domain.shared.paginated.DefaultPaginated;
import com.andaily.domain.shared.paginated.PaginatedSort;
import com.andaily.domain.shared.security.SecurityUtils;
import com.andaily.domain.user.AccountSetting;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Shengzhao Li
 */
public class AccountOverviewDTO extends DefaultPaginated<AccountDTO> {

    /**
     * Available:  all,self,latest
     */
    private AccountOverviewShowType showType;
    /**
     * Available: default,date,type
     */
    private AccountOverviewSortType sortType;

    private int total;

    private boolean showMore = true;

    public AccountOverviewDTO() {
    }

    public void checkDefaultTypes() {
        if (this.showType == null) {
            this.showType = AccountOverviewShowType.ALL;
        }
        if (this.sortType == null) {
            this.sortType = AccountOverviewSortType.DEFAULT;
        }
    }

    public AccountOverviewShowType getShowType() {
        return showType;
    }

    public void setShowType(AccountOverviewShowType showType) {
        this.showType = showType;
    }

    public AccountOverviewSortType getSortType() {
        return sortType;
    }

    public void setSortType(AccountOverviewSortType sortType) {
        this.sortType = sortType;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public boolean isShowMore() {
        return showMore;
    }

    public void setShowMore(boolean showMore) {
        this.showMore = showMore;
    }

    public Map<String, Object> queryParamMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("prePageSize", getPerPageSize());
        map.put("startIndex", getStartIndex());
        map.put("ownerGuid", SecurityUtils.currentUserGuid());

        checkDefaultTypes();
        map.put("showType", showType.name().toLowerCase());
        map.put("sortType", sortType.name().toLowerCase());
        return map;
    }

    public void updateShowMore() {
        this.showMore = isHasNext();
    }

    public void updateSetting(AccountSetting accountSetting) {
        this.setPerPageSize(accountSetting.preAccountPageSize());
        if (this.showType == null) {
            this.showType = accountSetting.accountOverviewShowType();
        }
        if (this.sortType == null) {
            this.sortType = accountSetting.accountOverviewSortType();
        }
    }
}
